<?php
class PostsController extends AppController {

	public $name = 'Posts';
	public $title = 'Default Title';

	function index() {
		$this->Post->recursive = 0;
		$this->set('posts', $this->paginate());
		$this->set('title', $this->title);
	}
	
	/***********************************************************
	 *  ,^^^'.     +---------------+
	 * c|  O0|_   /  make it work! |
	 *  |  --- |-------------------+
	 *  | /`~./
	 *  ||
	 ***********************************************************/
	function some_action() {
	
		// take first param, suppress warning if no params available
		$header = @$this->params['pass'][0];
		
		// take the rest of the params
		$arr = @array_slice( $this->params['pass'], 1 );

		// pass above variables or default if none available		
		$this->set('header', $header ? $header : 'Default header' );
		$this->set('arr', $arr ? $arr : array('Default array') );

		$dudes = $this->Post->Author->find('list');
		$this->set(compact( 'dudes' ));
		
		if( !empty( $this->data) ){
			$this->set( 'dump', $this->data );

			$this->Post->create();
			$this->Post->save($this->data);
			
			$this->Post->Author->create();
			$this->Post->Author->save($this->data);
			
			$this->Post->Test->create();
			$this->Post->Test->save($this->data);
		}
	}
	
	function home(){
		$this->set( 'index', array( 
							'title' => 'Welcome!', 
							'message' => '<p>This is the default action within the default controller... </p><ul><li>default controller = posts</li><li>default action = home</li></ul><p>To change these settings, edit /apps/config/routes.php</p>') 
					);
		$this->set( 'links', array(
						array( 'anchor' => 'Posts', 'href' => 'posts/' ),
						array( 'anchor' => 'Authors', 'href' => 'authors/' ),
						array( 'anchor' => 'Stuff', 'href' => 'posts/some_action/' ),
						array( 'anchor' => 'Go Live!', 'href' => 'r_customer_sites/dashboard/')
						)
					);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('post', $this->Post->read(null, $id));
		$this->set('title', 'Viewing');
	}

	function add() {
		if (!empty($this->data)) {
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		$authors = $this->Post->Author->find('list');
		$this->set(compact('authors'));
		$this->set('title', $this->title);
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		$authors = $this->Post->Author->find('list');
		$this->set(compact('authors'));
		$this->set('title', $this->title);
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for post', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Post->delete($id)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Post was not deleted', true));
		$this->redirect(array('action' => 'index'));
		$this->set('title', $this->title);
	}
}
?>